﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace School.Entities;

public partial class SchoolContext : DbContext
{
    public SchoolContext(DbContextOptions<SchoolContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Courses> Courses { get; set; }

    public virtual DbSet<Enrollments> Enrollments { get; set; }

    public virtual DbSet<Students> Students { get; set; }

    public virtual DbSet<Teacher> Teacher { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.UseCollation("Chinese_PRC_CI_AS");

        modelBuilder.Entity<Courses>(entity =>
        {
            entity.HasKey(e => e.Id).HasName("PK__Courses__C92D7187DFD60CA0");

            entity.ToTable(tb => tb.HasComment("课程表"));

            entity.Property(e => e.Id)
                .HasComment("课程编号")
                .HasColumnName("ID");
            entity.Property(e => e.Name)
                .HasMaxLength(50)
                .HasComment("课程名称");
            entity.Property(e => e.TeacherId)
                .HasComment("教师编号")
                .HasColumnName("TeacherID");

            //entity.HasOne(d => d.Teacher).WithMany(p => p.Courses)
            //    .HasForeignKey(d => d.TeacherId)
            //    .OnDelete(DeleteBehavior.ClientSetNull)
            //    .HasConstraintName("FK_Cource_Teacher");
        });

        modelBuilder.Entity<Enrollments>(entity =>
        {
            entity.HasKey(e => e.Id).HasName("PK__Enrollme__7F6877FBF8D05A53");

            entity.ToTable(tb => tb.HasComment("选课表"));

            entity.Property(e => e.Id)
                .HasComment("选课ID")
                .HasColumnName("ID");
            entity.Property(e => e.CourseId)
                .HasComment("课程ID")
                .HasColumnName("CourseID");
            entity.Property(e => e.StudentId)
                .HasComment("学生ID")
                .HasColumnName("StudentID");

            //entity.HasOne(d => d.Course).WithMany(p => p.Enrollments)
            //    .HasForeignKey(d => d.CourseId)
            //    .OnDelete(DeleteBehavior.ClientSetNull)
            //    .HasConstraintName("FK_Enrollments_Courses");

            //entity.HasOne(d => d.Student).WithMany(p => p.Enrollments)
            //    .HasForeignKey(d => d.StudentId)
            //    .OnDelete(DeleteBehavior.ClientSetNull)
            //    .HasConstraintName("FK_Enrollments_Students");
        });

        modelBuilder.Entity<Students>(entity =>
        {
            entity.HasKey(e => e.Id).HasName("PK__Students__32C52A790BE29C74");

            entity.ToTable(tb => tb.HasComment("学生表"));

            entity.Property(e => e.Id)
                .HasComment("学号")
                .HasColumnName("ID");
            entity.Property(e => e.Age).HasComment("年龄");
            entity.Property(e => e.Gender)
                .HasMaxLength(10)
                .HasComment("性别");
            entity.Property(e => e.Name)
                .HasMaxLength(50)
                .HasComment("姓名");
        });

        modelBuilder.Entity<Teacher>(entity =>
        {
            entity.HasKey(e => e.Id).HasName("PK__Teacher__3214EC27CC2A293F");

            entity.ToTable(tb => tb.HasComment("教师表"));

            entity.Property(e => e.Id)
                .HasComment("教师ID")
                .HasColumnName("ID");
            entity.Property(e => e.Name)
                .HasMaxLength(50)
                .HasComment("教师姓名");
        });

        OnModelCreatingPartial(modelBuilder);
    }

    // ============================       其他相关配置         ============================
    protected override void OnConfiguring (DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder.UseSqlServer("Server=127.0.0.1;Database=Botok;User=sa;Password=Pass1234;TrustServerCertificate=true");

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
